<?php

/**
 * Controller_summarize
 */

class Controller_summarize extends MyApp {

	public function __construct()
	{
		parent::__construct();

		$this->model->user     		= YOU::loadModel('user');
		$this->model->semester 		= YOU::loadModel('semester');
		$this->model->file		 	= YOU::loadModel('file');
		$this->model->department	= YOU::loadModel('department');
	}

	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}
	/** 部门计划列表 **/
	public function actionPlanCount()
	{

		// 查询条件
		$department_id   	= $this->get->department_id;
		$summarize_title	= $this->get->summarize_title;
		$from				= $this->get->from;
		$to					= $this->get->to;

		$where = " ";

		if(!empty($department_id)){
	
			$where .= " AND department_id ={$department_id} ";
			$this->tpl->assign("department_id", $department_id);
		}

		if(!empty($summarize_title)){
			$where .= " AND summarize_title LIKE '%{$summarize_title}%' ";
			$this->tpl->assign("summarize_title", $summarize_title);
		}
		if(!empty($from)){

			$where .= " AND create_time >= '{$from}' ";
			$this->tpl->assign("from", $from);
		}

		if(!empty($to)){

			$where .= " AND create_time <= '{$to}+1' ";
			$this->tpl->assign("to", $to);
		}
		// 部门
		$department_lists = $this->department_type;

		// 分页
		YOU::loadLib('page', "", '0');
		// summarize_mark=2 计划
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_summarize` WHERE summarize_mark = '2' {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$summarize_lists = $this->db->fetchAll("SELECT * FROM `tbl_summarize` WHERE summarize_mark = '2' {$where} ORDER BY summarize_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		foreach($summarize_lists as $key=>$value){

			//时间转换
			$summarize_lists[$key]['create_time']  = date('Y-m-d',strtotime($value['create_time']));

			// 学期、学年
			$row = $this->model->semester->getInfo($value['semester_id']);
			$summarize_lists[$key]['semester_xname'] = $row['semester_xname'];
			$summarize_lists[$key]['semester_name'] = $row['semester_name'];
	
			// 部门
			$summarize_lists[$key]['department_name'] = $this->department_type[$value['department_id']];
			
			// 控制标题字数
			$summarize_lists[$key]['summarize_title'] 	= mb_substr($value['summarize_title'],0,20,'utf8');
			if(strlen($summarize_lists[$key]['summarize_title'])>20){
				$summarize_lists[$key]['summarize_title']=$summarize_lists[$key]['summarize_title'].'...';
			}

		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("summarize_lists", $summarize_lists);
		$this->tpl->assign("department_lists", $department_lists);
		$this->tpl->display('summarize/plan.count.tpl.php');
	}
	/** 部门总结汇报列表 **/
	public function actionsummarizeCount()
	{
		// 查询条件
		$department_id   	= $this->get->department_id;
		$summarize_title	= $this->get->summarize_title;
		$from				= $this->get->from;
		$to					= $this->get->to;

		$where = " ";

		if(!empty($department_id)){
	
			$where .= " AND department_id ={$department_id} ";
			$this->tpl->assign("department_id", $department_id);
		}

		if(!empty($summarize_title)){
			$where .= " AND summarize_title LIKE '%{$summarize_title}%' ";
			$this->tpl->assign("summarize_title", $summarize_title);
		}
		if(!empty($from)){

			$where .= " AND create_time >= '{$from}' ";
			$this->tpl->assign("from", $from);
		}

		if(!empty($to)){

			$where .= " AND create_time <= '{$to}+1' ";
			$this->tpl->assign("to", $to);
		}
		// 部门
		$department_lists = $this->department_type;

		// 分页
		YOU::loadLib('page', "", '0');
		// summarize_mark=1 总结
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_summarize` WHERE summarize_mark = '1' {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$summarize_lists = $this->db->fetchAll("SELECT * FROM `tbl_summarize` WHERE summarize_mark = '1' {$where} ORDER BY summarize_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		foreach($summarize_lists as $key=>$value){
			//时间转换
			$value['create_time']  = date('Y-m-d',strtotime($value['create_time']));

			// 学期、学年
			$row = $this->model->semester->getInfo($value['semester_id']);
			$summarize_lists[$key]['semester_xname'] = $row['semester_xname'];
			$summarize_lists[$key]['semester_name'] = $row['semester_name'];

			// 部门
			$summarize_lists[$key]['department_name'] = $this->department_type[$value['department_id']];
			// 控制标题字数
			$summarize_lists[$key]['summarize_title'] 	= mb_substr($value['summarize_title'],0,20,'utf8');
			if(strlen($summarize_lists[$key]['summarize_title'])>20){
				$summarize_lists[$key]['summarize_title']=$summarize_lists[$key]['summarize_title'].'...';
			}

		}
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("summarize_lists", $summarize_lists);
		$this->tpl->assign("department_lists", $department_lists);
		$this->tpl->display('summarize/summarize.count.tpl.php');
	}
	/** --------------------------各个计划、总结----------------------------- **/
	/** 列表页 **/
	public function actionList ()
	{

		// 查询条件
		$summarize_title	= $this->post->summarize_title;
		$staff_name			= $this->post->staff_name;
		$from				= $this->post->from;
		$to					= $this->post->to;
		// 各计划、总结进入参数
		$summarize_type		= (int)$this->get->summarize_type;
		$summarize_mark		= (int)$this->get->summarize_mark;

		$where = " WHERE 1=1 ";

		if(!empty($summarize_title)){
			//模糊搜索
			$where .= " AND summarize_title LIKE '%{$summarize_title}%' ";
			$this->tpl->assign("summarize_title", $summarize_title);
		}

		if(!empty($staff_name)){

			$where .= " AND staff_id IN(SELECT `staff_id` FROM `tbl_staff` WHERE `staff_name` LIKE '%{$staff_name}%')";
			$this->tpl->assign("class_id", $class_id);
		}

		if(!empty($from)){

			$where .= " AND create_time >= '{$from}' ";
			$this->tpl->assign("from", $from);
		}

		if(!empty($to)){

			$where .= " AND create_time <= '{$to}+1' ";
			$this->tpl->assign("to", $to);
		}

		// 各个管理进入渠道
		if(!empty($summarize_type)){

			$where .= " AND summarize_type='{$summarize_type}' ";

		}

		// 通过权限获取老师的部门
		$staff_id		 = $this->user['type_id'];
		$staff_data		 = $this->db->fetchRow("SELECT `staff_name`,`department_id` FROM `tbl_staff`");
		$department_data = $this->db->fetchRow("SELECT `department_name`,`department_id` FROM `tbl_department` WHERE department_id= {$staff_data['department_id']}");

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_summarize` {$where}");

		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		$summarize_lists = $this->db->fetchAll("SELECT * FROM `tbl_summarize` {$where} ORDER BY summarize_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		//时间日期转换
		foreach($summarize_lists as $key=>$value){

			$summarize_lists[$key]['create_time']= date("Y-m-d", strtotime($summarize_lists[$key]['create_time']));
			// 学年学期
			$semester_data = $this->db->fetchRow("SELECT * FROM `tbl_semester` WHERE start_time <= '{$summarize_lists[$key]['create_time']}' AND end_time >= '{$summarize_lists[$key]['create_time']}' ");
			$summarize_lists[$key]['semester_xname'] = $semester_data['semester_xname'];
			$summarize_lists[$key]['semester_name'] = $this->semester_name[$semester_data['semester_name']];

			// 提交人
			$summarize_lists[$key]['staff_name'] = $this->model->user->getNameById($value['user_id']);
			// 控制标题字数
			$summarize_lists[$key]['summarize_title'] 	= mb_substr($value['summarize_title'],0,20,'utf8');
			if(strlen($summarize_lists[$key]['summarize_title'])>20){
				$summarize_lists[$key]['summarize_title']=$summarize_lists[$key]['summarize_title'].'...';
			}

		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("department_lists", $department_lists);
		$this->tpl->assign("summarize_lists", $summarize_lists);
		$this->tpl->assign("summarize_mark", $summarize_mark);
		$this->tpl->assign("summarize_type", $summarize_type);

		$this->tpl->assign("department_data", $department_data);

		//if($summarize_mark == 1){
		// 总结列表
		//	$this->tpl->display('summarize/summarize.list.tpl.php');
		//}else{
		// 计划
		$this->tpl->display('summarize/plan.list.tpl.php');
		//}

	}

	/** 添加 **/
	public function actionAdd()
	{
		// 各个计划、总结 参数
		$summarize_type	= (int)$this->get->summarize_type;
		$summarize_mark	= (int)$this->get->summarize_mark;

		// 获取权限
		$staff_id		 = $this->user['type_id'];
		$staff_data		 = $this->db->fetchRow("SELECT `staff_name`,`department_id` FROM `tbl_staff`");
		
		if ($_POST){

			// 当前学期
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];

			$summarize_title     	    = $this->post->summarize_title;
			$summarize_content   		= $this->post->summarize_content;
			$summarize_content = base64_encode($summarize_content);
			if (empty($summarize_title)){
				Helper_Client::alert("文章标题不能为空");
			}

			// 教务处
			if ($summarize_type == 1 || $summarize_type == 2) {
				$department_id = 1;
			}
			// 教研室
			if ($summarize_type == 5 || $summarize_type == 6) {
				$department_id = 2;
			}
			// 政教处
			if ($summarize_type == 9 || $summarize_type == 10) {
				$department_id = 3;
			}
			// 团支部
			if ($summarize_type == 7 || $summarize_type == 8) {
				$department_id = 4;
			}
			// 总务处
			if ($summarize_type == 11 || $summarize_type == 12) {
				$department_id = 5;
			}
			// 写入数据库
			$info = array(
			"user_id"  	    => $this->user['user_id'],
			"summarize_title"  	    => $summarize_title,
			"summarize_content"     => $summarize_content,
			// 部门id
			"department_id"			=> $department_id,
			// 各个管理的参数
			"summarize_type"       	=> $summarize_type,
			// 1工作总结，2=工作计划
			"summarize_mark"       	=> $summarize_mark,
			"semester_id"           => $semester_id,
			"status"               => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_insert("tbl_summarize", $info);
			$summarize_id = $this->db->insert_id();

			// 附件上传
			$file = $this->model->file->upload("summarize_file", $summarize_id, array("txt","ppt", "xls", "xlsx", "docs", "docx", "doc", "pdf", "jpg", "png", 
"pdf", "gif", "zip", "rar","mp4","bov","wmv","dvd","rmvb","avi","rm",));
			if (!empty($file)){
				$info = array(
				"summarize_file" =>  $file['fname'],
				);

				$this->db->do_update("tbl_summarize", $info, "summarize_id='{$summarize_id}'");
			}
			// 计划、部分跳转位置
			Helper_Client::location("添加成功", "index.php?a=summarize.list&summarize_mark={$summarize_mark}&summarize_type={$summarize_type}");

		}

		$this->tpl->display("summarize/add.tpl.php");
	}

	/** 修改 **/
	public function actionUpdate()
	{
        $uid = $this->user['user_id'];
        $this->tpl->assign('uid',$uid);
		$summarize_id		= (int)$this->get->summarize_id;

		// 各个计划、总结 参数
		$summarize_type	= (int)$this->get->summarize_type;
		$summarize_mark	= (int)$this->get->summarize_mark;

		$summarize_data		= $this->db->fetchRow("SELECT * FROM `tbl_summarize` WHERE `summarize_id`='{$summarize_id}'");
		$summarize_data['summarize_content'] = base64_decode($summarize_data['summarize_content']);

		if (empty($summarize_data)){
			Helper_Client::alert("文章不存在");
		}
        $uid = $this->user['user_id'];
        if($summarize_data['user_id']&& ($uid !=$summarize_data['user_id']) ){
            Helper_Client::alert("对不起无此权限");
            exit;
        }

		if ($_POST){

			$summarize_title     	    = $this->post->summarize_title;
			$summarize_content   		= $this->post->summarize_content;
			$summarize_content = base64_encode($summarize_content);
			if (empty($summarize_title)){
				Helper_Client::alert("标题不能为空");
			}

			// 写入数据库
			$info = array(

			"summarize_title"  	    => $summarize_title,
			"summarize_content"     => $summarize_content,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_update("tbl_summarize", $info, "summarize_id='{$summarize_id}'");

            // 附件上传
            if ($this->post->summarize_file_re=='1'){
                // 附件上传
                $file = $this->model->file->upload("summarize_file", $summarize_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));

                if (!empty($file)){
                    $info = array(
                        "summarize_file" => $file['fname'],
                    );

                    $this->db->do_update("tbl_summarize", $info, "summarize_id='{$summarize_id}'");
                }
            }
			// 跳转
			Helper_Client::location("修改成功", "index.php?a=summarize.list&summarize_mark={$summarize_mark}&summarize_type={$summarize_type}");
		}

		$this->tpl->assign("summarize_data", $summarize_data);
		$this->tpl->display("summarize/update.tpl.php");
	}

	/** 详情页 **/
	public function actionManageClassifyInfoList ()
	{
			
		$summarize_id   = (int)$this->get->summarize_id;
		$summarize_data = $this->db->fetchRow("SELECT * FROM `tbl_summarize` WHERE `summarize_id`='{$summarize_id}'");
		$summarize_data['summarize_content'] = base64_decode($summarize_data['summarize_content']);
		if (empty($summarize_data)){
			Helper_Client::alert("文章不存在");
		}
		// 提交者
		$summarize_data['user_name'] = $this->model->user->getNameById($summarize_data['user_id']);
		// 时间
		$summarize_data['create_time'] = date('Y-m-d',strtotime($summarize_data['create_time']));
		
		$this->tpl->assign("summarize_data", $summarize_data);
		$this->tpl->assign("summarize_pc", $summarize_pc);
		$this->tpl->display('summarize/particulars.tpl.php');
	}

	/** 删除 **/
	public function actionDel()
	{
		$summarize_id   = $this->get->summarize_id;
        $summarize_data		= $this->db->fetchRow("SELECT * FROM `tbl_summarize` WHERE `summarize_id`='{$summarize_id}'");


        if (empty($summarize_data)){
            Helper_Client::alert("文章不存在");
        }
        $uid = $this->user['user_id'];
        if($summarize_data['user_id']&& ($uid !=$summarize_data['user_id']) ){
            Helper_Client::alert("对不起无此权限");
            exit;
        }
		$summarize_type	= (int)$this->get->summarize_type;
		$summarize_mark	= (int)$this->get->summarize_mark;
		$this->db->query("DELETE  FROM `tbl_summarize` WHERE summarize_id='{$summarize_id}'");
		// 跳转
		Helper_Client::location("删除成功", "index.php?a=summarize.list&summarize_mark={$summarize_mark}&summarize_type={$summarize_type}");
	}
	/** 下载**/
	public function actionoutput(){
		$fname = $this->get->summarize_file;
		$fpath = UPLOAD_PATH."/summarize_file/{$fname}";

		// 输出下载文件头s
		header("Content-Disposition: attachment; filename={$fname}");
		// 在线输出
		readfile($fpath);

	}
}

?>